import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class Refresh extends StatelessWidget {
  /// 控制器
  final RefreshController controller;

  /// 子组件
  final Widget? child;

  /// 刷新头部
  final Widget? header;

  /// 刷新尾部
  final Widget? footer;

  /// 是否允许下拉刷新
  final bool enablePullDown;

  /// 是否允许上拉加载
  final bool enablePullUp;

  /// 刷新回调
  final VoidCallback? onRefresh;

  /// 加载回调
  final VoidCallback? onLoading;

  /// 用于SingleChildView设置
  final bool? primary;

  /// 用于SingleChildView设置
  final ScrollPhysics? physics;

  /// 用于SingleChildView设置
  final Axis? scrollDirection;

  /// 用于SingleChildView设置
  final ScrollController? scrollController;

  /// 是否Material头部
  final bool isMaterialHeader;

  /// 没有数据显示内容
  final String? noDataText;

  const Refresh({
    Key? key,
    required this.controller,
    this.child,
    this.header,
    this.footer,
    this.enablePullDown = true,
    this.enablePullUp = false,
    this.onRefresh,
    this.onLoading,
    this.primary,
    this.physics,
    this.scrollDirection,
    this.scrollController,
    this.isMaterialHeader = false,
    this.noDataText,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: controller,
      header: header ?? _buildDefaultHeader(),
      footer: footer ?? _buildFooter(),
      enablePullDown: enablePullDown,
      enablePullUp: enablePullUp,
      onRefresh: onRefresh,
      onLoading: onLoading,
      primary: primary,
      physics: physics,
      scrollDirection: scrollDirection,
      scrollController: scrollController,
      child: child,
    );
  }

  /// 刷新尾部组件
  Widget _buildDefaultHeader() {
    return isMaterialHeader
        ? const MaterialClassicHeader()
        : ClassicHeader(
            refreshStyle: RefreshStyle.UnFollow,
            textStyle: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w400,
              color: Colors.grey,
            ),
            idleText: "",
            releaseText: "",
            refreshingText: "",
            completeText: "",
            spacing: 0,
            iconPos: IconPosition.left,
            refreshingIcon: _buildIndicator(),
            failedIcon: _buildIndicator(),
            completeIcon: _buildIndicator(),
            idleIcon: _buildIndicator(),
            releaseIcon: _buildIndicator(),
          );
  }

  /// 刷新尾部组件
  Widget _buildFooter() {
    return ClassicFooter(
      textStyle: TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w400,
        color: Colors.grey,
      ),
      idleText: "上拉加载更多",
      canLoadingText: "松手加载更多",
      loadingText: "正在加载...",
      noDataText: noDataText ?? "暂无更多",
      idleIcon: const SizedBox(),
      canLoadingIcon: const SizedBox(),
      loadingIcon: _buildIndicator(),
    );
  }

  Widget _buildIndicator() {
    return SizedBox(
      width: 24.w,
      height: 24.w,
      child: CircularProgressIndicator(
        backgroundColor: Colors.transparent,
        color: Colors.grey,
        strokeWidth: 1.5,
      ),
    );
  }
}
